<%@ page import = "ro.gateway.aida.obj.*,
                   ro.gateway.aida.utils.Utils,
                   ro.gateway.aida.db.PersistenceToken,
                   ro.gateway.aida.db.PersistenceManager" %>
<%@ page import = "ro.gateway.aida.obj.db.*" %>
<%@ page import = "ro.gateway.aida.servlet.*" %>
<%@ page import = "ro.gateway.aida.srv.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "java.util.Hashtable" %>

<%@ taglib uri="/WEB-INF/taglib.tld" prefix="gtw" %>
<%@ taglib uri="/WEB-INF/translator.tld" prefix="trans" %>
<%@ taglib uri="/WEB-INF/usr.tld" prefix="usr" %>

<%
          Hashtable services = (Hashtable) application.getAttribute(Constants.SERVICES);
          if (services == null) {
               System.err.println("services null...  locselector");
               return;
          }
          ICFService cfService = (ICFService) services.get(Constants.SERVICE_CF);
	  // 1 - region
	  // 2 - County
	  // 3 - Town
	  // 4 - Village
	  int detail_level = Utils.getInt(request, "level", 4);
	  if (detail_level < 1) detail_level = 1;
	  if (detail_level > 4) detail_level = 4;

	  int region_id = Utils.getInt(request, "rid", -1);
	  int County_id = Utils.getInt(request, "jid", -1);
	  int Town_id = Utils.getInt(request, "oid", -1);
	  int Village_id = Utils.getInt(request, "lid", -1);
	  // 1 - regiune, 2 - County, 3 - Town
	  int field_modified = Utils.getInt(request, "fmdf", 0);
	  if (field_modified == 1) {County_id=-1; Town_id=-1; Village_id=-1;}
	  if (field_modified == 2) {Town_id=-1; Village_id=-1;}
	  if (field_modified == 3) {Village_id=-1;}

	  String key = Utils.getValidTrimedString(request, "key", null);
	  String country=Utils.getValidTrimedString(request,"country",null);



	  if (key == null) return;

	Region[] regiuni = new Region[0];
	County[] Counties = new County[0];
	Town[] Towne = new Town[0];
	Village[] localitati = new Village[0];
      PersistenceToken token = PersistenceManager.tokenLookup( application );
	try {
		while (true) {
			regiuni = RegionDB.getManager(token ).getAllByCountry(country);
			//regiuni = RegionDB.getAll(cfService, Constants.DBS1);
			if (regiuni==null) break;
			if (detail_level >= 2) {
				Counties = CountyDB.getManager(token ).getForRegion(region_id);
				if (Counties == null) {
					region_id = regiuni[0].getId();
				}
				Counties = CountyDB.getManager(token ).getForRegion(region_id);
				if (Counties == null) {
					Counties = new County[0];
				}
			} else break;

			if (detail_level >= 3) {
				if (County_id == -1) {
					County_id = (Counties.length > 0) ? Counties[0].getId() : -1;
				}
				Towne = TownDB.getManager(token ).getAllForCounty(County_id);
				if (Towne == null) {
					Towne = new Town[0];
				}
			} else break;

			if (detail_level >= 4) {
				if (Town_id == -1) Town_id = (Towne.length > 0) ? Towne[0].getSirsup_id() : -1;
				localitati = VillageDB.getManager(token ).getAllForTown(Town_id);
				if (localitati == null) {
					localitati = new Village[0];
				}
			} else break;

			break;
		}
	} catch (SQLException sqlEx) {
		sqlEx.printStackTrace();
	}
%>

<trans:initPage module="prj_pages" />
<html>
<head>
<title>Romania Development Gateway - AIDA - Location selector</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="alf.css" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000">
        <table width="90%" border="0" cellspacing="1" cellpadding="5" align="center" bgcolor="#CCCCCC">
		<form method="POST" name="theform">
		<input type="hidden" name="level" value="<%=detail_level%>" />
		<input type="hidden" name="fmdf" value="" />
		<input type="hidden" name="key" value="<%=key%>" />
		<tr bgcolor="f9f9f9">
			<td colspan="2" width="39%" class="text_search"><b><trans:showPageMessage label="loc.select" /></b></td>
		</tr>

		<% if (regiuni!=null) { %>
		<% if (detail_level >= 1) { %>
		<tr bgcolor="f9f9f9">
			<td class="text_search" width="20%"><trans:showPageMessage label="loc.region" /></td>
			<td class="text_search" width="80%">
				<select name="rid" <%=(detail_level > 1) ? "onChange=\"javascript:document.theform.fmdf.value=1;document.theform.submit();\"":""%>>
				<% for (int i = 0; i < regiuni.length; i++) { %>
				<option value="<%=regiuni[i].getId()%>" <%=(regiuni[i].getId() == region_id) ? "selected=\"true\"" : ""%>><%=regiuni[i].getName()%></option>
				<% } %>
				</select>
			</td>
		</tr>
		<% } %>
		<% if (detail_level >= 2) { %>
		<tr bgcolor="f9f9f9">
			<td class="text_search"><trans:showPageMessage label="loc.county" /></td>
			<td class="text_search">
				<select name="jid" <%=(detail_level > 2) ? "onChange=\"javascript:document.theform.fmdf.value=2;document.theform.submit();\"":""%>>
				<% for (int i = 0; i < Counties.length; i++) { %>
				<option value="<%=Counties[i].getId()%>" <%=(Counties[i].getId() == County_id) ? "selected=\"true\"" : ""%>><%=Counties[i].getName()%></option>
				<% } %>
				</select>
			</td>
		</tr>
		<% } %>
		<% if (detail_level >= 3) { %>
		<tr bgcolor="f9f9f9">
			<td class="text_search"><trans:showPageMessage label="loc.munic" /><br>/<trans:showPageMessage label="loc.city" /><br>/<trans:showPageMessage label="loc.town" /></td>
			<td class="text_search">
				<select name="oid" <%=(detail_level > 3) ? "onChange=\"javascript:document.theform.fmdf.value=3;document.theform.submit();\"":""%>>
				<% for (int i = 0; i < Towne.length; i++) { %>
				<option value="<%=Towne[i].getSirsup_id()%>" <%=(Towne[i].getSirsup_id() == Town_id) ? "selected=\"true\"" : ""%>><%=Towne[i].getName()%> - <%=(Towne[i].getType() == 1) ? "Municipiu " : ((Towne[i].getType() == 2) ? "Town " : "Comuna ")%></option>
				<% } %>
				</select>
			</td>
		</tr>
		<% } %>
		<% if (detail_level >= 4) { %>
		<tr bgcolor="f9f9f9">
			<td class="text_search"><trans:showPageMessage label="loc.village" /></td>
			<td class="text_search">
				<select name="lid">
				<% for (int i = 0; i < localitati.length; i++) { %>
				<option value="<%=localitati[i].getSiruta_id()%>" <%=(localitati[i].getSiruta_id() == Village_id) ? "selected=\"true\"" : ""%>><%=localitati[i].getName()%></option>
				<% } %>
				</select>
			</td>
		</tr>
		<% } %>

		<% }//regiuni %>

		</form>
		<script language="javascript">
		function go_back() {
			var url = "editActivity?key=" + document.theform.key.value + "&page=2" +
			"&country=<%=country%>&action=add_loc&rid=" + document.theform.rid.options[document.theform.rid.selectedIndex].value;
			if (document.theform.jid != null) {
				url += "&jid=" + document.theform.jid.options[document.theform.jid.selectedIndex].value;
			}
			if (document.theform.oid != null) {
				url += "&oid=" + document.theform.oid.options[document.theform.oid.selectedIndex].value;
			}
			if (document.theform.lid != null) {
				url += "&lid=" + document.theform.lid.options[document.theform.lid.selectedIndex].value;
			}
			opener.location = url;
			window.close();
		}
		</script>
		<tr bgcolor="f9f9f9">
			<td class="text_search" colspan="2" align="center">
			<% if (regiuni!=null) { %>
			<input type="button" value="Select" onclick="javascript:go_back();" >
			<% } else {%>
				<trans:showPageMessage label="loc.nofound" />
			<% } %>
			<input type="button" onclick="javascript:window.close();" value="Cancel">
			</td>
		</tr>
	</table>
</body>
</html>